﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Collections.Specialized;
using Facebook;
using System.Web.Script.Serialization;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public string app_id = "188715331198022";
        public string app_secret = "47b66709d0fded0d727b615aa7faeda2";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["code"] != null)
            {
                Session["code"] = Request.QueryString["code"];
                TextBox1.Text = Session["code"].ToString();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            object tmp;
            string token_url = "https://graph.facebook.com/oauth/access_token?client_id=" +
                app_id + "&redirect_uri=http://bestgift4us.com.winhostserver.com/WebForm1.aspx" +
                "&client_secret=" + app_secret + "&code=" + TextBox1.Text;

            string Response = GetUrl(token_url); 
            NameValueCollection qscoll = HttpUtility.ParseQueryString(Response);
            string feed_url= "https://graph.facebook.com/me/feed/?access_token=" + qscoll["access_token"];

           // string feed_json = GetUrl(feed_url);
            JavaScriptSerializer ser = new JavaScriptSerializer();
            FacebookClient client = new FacebookClient(qscoll["access_token"]);
            var me = client.Get("me/friends");


            IDictionary<string, object>  me2 = (IDictionary<string, object> ) client.Get("me/feed");
            for (int i = 1; i < 10; i++)
            {
                string nexturl = ((IDictionary<string, object>)me2["paging"])["next"].ToString();
                NameValueCollection until = HttpUtility.ParseQueryString(nexturl);

                me2 = (IDictionary<string, object>)client.Get("me/feed?until=" + until["until"]);
                foreach (IDictionary<string, object> posts in (Facebook.JsonArray)me2["data"])
                {
                    if (posts.TryGetValue("message", out tmp))
                        TextBox2.Text = TextBox2.Text + "\r\n" + posts["message"];
                }
                TextBox2.Text = TextBox2.Text + "\r\n" + "____________________";
            }
            //var   feed_data =  (IDictionary<string, object>) ( me2["data"] );


             
            
        }

        private static string GetUrl(string token_url)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(token_url);
            Encoding enc = System.Text.Encoding.GetEncoding(1252);
            string Response = new StreamReader(((HttpWebResponse)req.GetResponse()).GetResponseStream(), enc).ReadToEnd();
            return Response;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           
        }
    }
}